#include <dbg.h>

#include <cassert>

using namespace std;

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int n = nums.size();
        int res = n + 1;
        int sum = 0;
        int left = 0;
        for (int right = 0; right < n; ++right) {
            sum += nums[right];
            while (sum >= target) {
                res = min(res, right - left + 1);
                sum -= nums[left];
                ++left;
            }
        }
        return res == n + 1 ? 0 : res;
    }
};

int main() {
    Solution s;

    vector<int> nums1{2, 3, 1, 2, 4, 3};
    vector<int> nums2{1, 4, 4};
    vector<int> nums3{1, 1, 1, 1, 1, 1, 1, 1};

    assert(s.minSubArrayLen(7, nums1) == 2);
    assert(s.minSubArrayLen(4, nums2) == 1);
    assert(s.minSubArrayLen(11, nums2) == 0);

    return 0;
}